//
//  FindCommodityCell.m
//  WOQIEarjoy
//
//  Created by 孙鹏飞 on 2023/11/24.
//  Copyright © 2023 WOQI. All rights reserved.
//

#import "FindCommodityCell.h"

#import <BIToolModule/BIProjectDefine.h>
#import <SDWebImage/SDWebImage.h>

@interface FindCommodityCell()

@property(nonatomic,strong) NSMutableArray * dataSource;
@property(nonatomic,strong) UIImageView * imagV ;
@property(nonatomic,strong) UILabel * titleLab ;

@property(nonatomic,strong) UILabel * yjLab ;//原价
@property(nonatomic,strong) UILabel * yjLab2 ;//
@property(nonatomic,strong) UILabel * ysLab ;//已售
@property(nonatomic,strong) UILabel * ysLab2 ;//
@property(nonatomic,strong) UILabel * dsjLab ;//到手价
@property(nonatomic,strong) UIButton * btn ;//

@end


@implementation FindCommodityCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
//        self.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
//        self.layer.borderWidth = 1;
    }
    return self;
}

- (void)setUI{
    _imagV = [[UIImageView alloc]init];
//    _imagV.backgroundColor = [UIColor whiteColor];
    _imagV.layer.cornerRadius = 10;
    _imagV.layer.masksToBounds = YES;
    _imagV .frame = CGRectMake(0, 0, (BIScreenWidth-52)/2,   (BIScreenWidth-52)/2);
//    _imagV .image = [UIImage imageNamed:@"ic／16／next"];
    [self.contentView addSubview:_imagV];
    
    _titleLab = [[UILabel alloc]init];
    _titleLab.frame = CGRectMake(5, (BIScreenWidth-52)/2,(BIScreenWidth - 52)/2-10, 40);
//    _titleLab.text = @"右";
    _titleLab.textAlignment = NSTextAlignmentLeft;
    _titleLab.font = [UIFont systemFontOfSize:13];
    _titleLab.textColor = [UIColor blackColor];
    _titleLab.numberOfLines =2;
    [self.contentView addSubview:_titleLab];

    _yjLab = [[UILabel alloc]init];
    _yjLab.frame = CGRectMake(5, (BIScreenWidth-52)/2+70,35, 30);
//    _yjLab.text = NTVLocalized(@"原价:", nil);
    _yjLab.text = BILocalizedString(@"原价:");
    _yjLab.textAlignment = NSTextAlignmentLeft;
    _yjLab.font = [UIFont systemFontOfSize:12];
    _yjLab.textColor = [UIColor blackColor];
    [self.contentView addSubview:_yjLab];
    
    
    _yjLab2 = [[UILabel alloc]init];
    _yjLab2.frame = CGRectMake(40 ,  (BIScreenWidth-52)/2+70,(BIScreenWidth-52)/4-40, 30);
//    _yjLab2.text = @"右";
    _yjLab2.textAlignment = NSTextAlignmentLeft;
    _yjLab2.font = [UIFont systemFontOfSize:12];
    _yjLab2.textColor = [UIColor grayColor];
    [self.contentView addSubview:_yjLab2];
    
    _ysLab = [[UILabel alloc]init];
    _ysLab.frame = CGRectMake((BIScreenWidth-52)/4+5,  (BIScreenWidth-52)/2+70,30, 30);
//    _ysLab.text = NTVLocalized(@"已售:", nil);
    _ysLab.text = BILocalizedString(@"已售:");
    _ysLab.textAlignment = NSTextAlignmentLeft;
    _ysLab.font = [UIFont systemFontOfSize:12];
    _ysLab.textColor = [UIColor blackColor];
    [self.contentView addSubview:_ysLab];
    
    _ysLab2 = [[UILabel alloc]init];
    _ysLab2.frame = CGRectMake((BIScreenWidth-52)/4+35,  (BIScreenWidth-52)/2+70,(BIScreenWidth-52)/4-40, 30);
//    _ysLab2.text = @"右";
    _ysLab2.textAlignment = NSTextAlignmentLeft;
    _ysLab2.font = [UIFont systemFontOfSize:12];
    _ysLab2.textColor = [UIColor grayColor];
    [self.contentView addSubview:_ysLab2];
    
    
    _dsjLab = [[UILabel alloc]init];
    _dsjLab.frame = CGRectMake(5,  (BIScreenWidth-52)/2+40,(BIScreenWidth-52)/2-10, 30);
//    _dsjLab.text = [NSString stringWithFormat:@"￥0 %@",NTVLocalized(@"到手价", nil)];
    _dsjLab.text = [NSString stringWithFormat:@"￥0 %@",BILocalizedString(@"到手价")];
    _dsjLab.textAlignment = NSTextAlignmentLeft;
    _dsjLab.font = [UIFont systemFontOfSize:12];
    _dsjLab.textColor = [UIColor redColor];
    [self.contentView addSubview:_dsjLab];

    
    
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(0, 0, (BIScreenWidth-52)/2,  (BIScreenWidth-52)/2+100);
    _btn.backgroundColor = [UIColor clearColor];
    [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_btn];
    
    
    
    
    
}
-(void)setModel:(BannerModel *)model{
    _model = model;
    [self.imagV sd_setImageWithURL:[NSURL URLWithString:model.image]];
    _titleLab.text = _model.title;
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",model.originalPrice] attributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleNone)}];
    [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, attri.length)];
    [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, attri.length)];
    self.yjLab2.attributedText = attri;
//    DLog(@"---model.salesQuantity---%@",model.salesQuantity);
    
    if ([model.salesQuantity floatValue]>10000 && [model.salesQuantity floatValue]<100000) {
      float n = [model.salesQuantity floatValue]/10000;
        self.ysLab2.text = [NSString stringWithFormat:@"%.1fW+",n];
    }else if ([model.salesQuantity floatValue]>100000){
        float n = [model.salesQuantity floatValue]/10000;
        self.ysLab2.text = [NSString stringWithFormat:@"%.0fW+",n];
    } else{
        self.ysLab2.text = model.salesQuantity;
    }
    
//    NSString * str = [NSString stringWithFormat:@"￥%@ %@",model.preferentialPrice,NTVLocalized(@"到手价", nil)];
    NSString * str = [NSString stringWithFormat:@"￥%@ %@",model.preferentialPrice, BILocalizedString(@"到手价")];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:18] range:NSMakeRange(1, model.preferentialPrice.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(1,model.preferentialPrice.length)];
    self.dsjLab.attributedText = attributedString;
    
}
-(void)btnClick{
    if (_model.redirectPath) {
        [self jumpUrl:_model.redirectPath];
    }
    NSDictionary * dic = @{@"ids":_model.ids};
//    [MobClick event:@"click_find_product" attributes:dic];
}
-(void)jumpUrl:(NSString *)url{
    if ([url rangeOfString:@"jd.com"].location != NSNotFound) {
        
        NSString * jdUrl =[[NSString stringWithFormat:@"openapp.jdmobile://virtual?params={\"category\":\"jump\",\"des\":\"m\",\"url\":\"%@\",\"keplerID\":\"0\",\"keplerFrom\":\"1\",\"kepler_param\":{\"source\":\"kepler-open\",\"otherData\":{\"mopenbp7\":\"0\"}},\"union_open\":\"union_cps\"}",url] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"openapp.jdmobile://"]]) {
//            NSLog(@"安装了京东");
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:jdUrl] options:@{} completionHandler:nil];
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
        }

    }else if ([url rangeOfString:@"taobao.com"].location != NSNotFound) {
        NSString *str = [url stringByReplacingOccurrencesOfString:@"https" withString:@"tbopen"];
        NSURL *tbUrl = [NSURL URLWithString:str];
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tbopen://"]]) {
            [[UIApplication sharedApplication] openURL:tbUrl options:@{} completionHandler:nil];
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
        }

    }
//    else if ([url rangeOfString:@"tmall.com"].location != NSNotFound) {
//        NSString *str = [url stringByReplacingOccurrencesOfString:@"https" withString:@"tmall"];
//        NSURL *tbUrl = [NSURL URLWithString:str];
//        if ([[UIApplication sharedApplication] canOpenURL:tbUrl]) {
//            [[UIApplication sharedApplication] openURL:tbUrl options:@{} completionHandler:nil];
//        }else{
//            //直接跳转html
//            [[UIApplication sharedApplication] openURL:tbUrl options:@{} completionHandler:nil];
//        }
//    }
    else{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
    }
}

@end
